package service

import (
	"fmt"
	"math/rand"
	"rongyy/src/helper"
	"rongyy/src/middlewares"
	"rongyy/src/models"
	"time"
)

type WxSubscribeMsgService struct {
}

// 挂号成功通知
func (ps *WxSubscribeMsgService) SendSupplierMsg(result string, openid string, page, remark string) error {
	bi := &models.SubMsgDataModel{}
	bi.Phrase1.Value = "供应商预约"
	bi.Phrase2.Value = result
	bi.Date3.Value = time.Now().Format("2006-01-02")
	bi.Thing4.Value = remark
	err := ps.SendSubMsg(bi, openid, "供应商申请通知", page)
	if err != nil {
		middlewares.Logger.Println("挂号成功通知 error==== ", err)
		//return err
	}
	return nil
}

// 公共方法
func (ps *WxSubscribeMsgService) SendSubMsg(data *models.SubMsgDataModel, openid string, name string, page string) error {
	token, err := (&WxConfService{}).GetToken()
	if err != nil {
		return err
	}
	fmt.Println(token)
	if token != "" {
		rand.Seed(time.Now().UnixNano())
		//t := rand.Intn(10000)
		//ts := strconv.FormatInt(time.Now().UnixMicro(), 10)
		req := &models.SubMsgModel{
			Touser:           openid,
			TemplateId:       "BQfeOVCXmX3TFmwDKEDsCNN0xfF2_XUKuS9X1nuJ8p8",
			Page:             page,
			MiniprogramState: "",
			Lang:             "",
			Data:             data,
		}

		res, err := helper.PostJson("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+token, req)
		jy := &models.JyMsgModel{
			PatientId: "",
			Openid:    openid,
			Type:      name,
			Result:    "发送成功",
		}
		middlewares.Logger.Println(string(res))
		middlewares.Logger.Println(err)
		if err != nil {
			return err
			jy.Result = err.Error()
		}
		jy.Insert()
		return nil
	}
	return fmt.Errorf("token为空，请更新")
}
